<template>
  <div class="app-container">
    <el-button type="primary" @click="dialogFormVisible = true">发布</el-button>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="ID" width="95">
        <template slot-scope="scope">{{ scope.$index+1 }}</template>
      </el-table-column>
      <el-table-column label="发送内容" width="500" prop="text" />
      <el-table-column label="微博ID" width="150" prop="idstr" />
      <el-table-column prop="created_at" label="发布时间" width="220">
        <template slot-scope="scope">
          <i class="el-icon-time" /><span>{{ scope.row.created_at| timeFilter }}</span><br>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="发送微博"
      :visible.sync="dialogFormVisible"
      width="60%"
      center>
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="发送内容" :label-width="formLabelWidth" prop="content">
          <el-input v-model="form.content" autocomplete="off" style="width:200px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureChange">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { sendList,sendWeiBo } from '@/api/weibo'
import { parseTime } from '@/utils'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    timeFilter(time) {
      return parseTime(time)
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      dialogFormVisible: false,
      dialogRowFormVisible: false,
      tableData: [],
      form: {
        content: ''
      },
      rowForm: {},
      formLabelWidth: '120px',
      rules: {
        content: [
          { required: true, message: '必填', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      this.listLoading = true
      const { data } = await sendList()
      this.list = data.statuses
      this.listLoading = false
    },

    async sureChange() {
      this.dialogFormVisible = false;
      try {
        await sendWeiBo(this.form);
        this.$message({
          message: '怎么回事小老弟',
          type: 'success'
        })
      }catch (err) {
        this.$message({
          message: err,
          type: 'error'
        })
      }

    }
  }
}
</script>
